home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / strlib.zip / _STR2MAP.C < prev    next >
Text File  |  1993-01-04  |  3KB  |  75 lines

  1.  
  2. /*  File   : _str2map.c
  3.     Author : Richard A. O'Keefe.
  4.     Updated: 20 April 1984
  5.     Defines: _map_vec[], _str2map().
  6.  
  7.     _str2map(option, from, to) constructs a translation table.  If  from
  8.     or to is NullS, the same string is used as last time, so if you want
  9.     to translate a whole lot of strings using the same mapping you don't
  10.     have to reconstruct it each time.  The options are
  11.  
  12.         0: initialise the map to the identity function,
  13.            then map each from[i] to the corresponding to[i].
  14.            If to[] is shorter than from[], its last character is
  15.            repeated as often as needed.
  16.  
  17.         1: as 0, but don't initialise the map.
  18.  
  19.         2: initialise the map to send every character to to[0],
  20.            then map each from[i] to itself.
  21.  
  22.     For example, to build a map which forces letters to lower case but
  23.     sends everything else to blank, call
  24.  
  25.         _str2map(2, "abcdefghijklmnopqrstuvwxyz", " ");
  26.         _str2map(1, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz");
  27.     Only strtrans() and strntrans() in this package  call  _str2map;  if
  28.     you  want  to  build your own maps this way you can "fool" them into
  29.     using it, as when the two strings are NullS they  don't  change  the
  30.     map.   As an extra-special dubious *hack*, _map_vec has an extra NUL
  31.     character at the end, so after calling _str2map(0, "", ""), you  can
  32.     use  _map_vec+1 as a string of the 127 non-NUL characters (or if the
  33.     _AlphabetSize is 256, of the 255 non-NUL characters).
  34. */
  35.  
  36. #include "strings.h"
  37. #include "_str2map.h"
  38.  
  39. static  _char_  *oldFrom = "?";
  40. static  char    *oldTo   = "?";
  41.  
  42. char    _map_vec[_AlphabetSize+1];
  43.  
  44. void _str2map(option, from, to)
  45.     int option;
  46.     register _char_ *from;
  47.     register char *to;
  48.     {
  49.         register int i, c;
  50.  
  51.         if (from == NullS && to == NullS) return;
  52.         if (from == NullS) from = oldFrom; else oldFrom = from;
  53.         if (to   == NullS) to   = oldTo;   else oldTo   = to;
  54.         switch (option) {
  55.             case 0:
  56.                 for (i = _AlphabetSize; --i >= 0; _map_vec[i] = i) ;
  57.             case 1:
  58.                 while (i = *from++) {
  59.                     _map_vec[i] = *to++;
  60.                     if (!*to) {
  61.                         c = *--to;
  62.                         while (i = *from++) _map_vec[i] = c;
  63.                         return;
  64.                     }
  65.                 }
  66.                 return;
  67.             case 2:
  68.                 c = *to;
  69.                 for (i = _AlphabetSize; --i >= 0; _map_vec[i] = c) ;
  70.                 while (c = *from++) _map_vec[c] = c;
  71.                 return;
  72.         }
  73.     }
  74.  
  75.